package br.ufpe.cin.rdfilter.gui;

import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.WindowConstants;

import br.ufpe.cin.rdfilter.feedback.Annotation;
import br.ufpe.cin.rdfilter.feedback.Triple;

/**
 *
 * @author Rafael
 */
public class FrameAddEdit extends javax.swing.JFrame {

	private javax.swing.ButtonGroup buttonGroup;
	private javax.swing.JButton btnSave;
	private javax.swing.JButton btnBack;
	private javax.swing.JSpinner confidence;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JRadioButton rgFalsePositive;
	private javax.swing.JRadioButton rgTruePositive;
	private javax.swing.JRadioButton rgFalseNegative;
	private javax.swing.JTextField tfSubject;
	private javax.swing.JTextField tfPredicate;
	private javax.swing.JTextField tfObject;
	
	private static final long serialVersionUID = 1L;
	
	private Triple selectedTriple = null;
	private Annotation selectedAnnotation = null;
	private FrameTool frameTool;
	private FrameAnnotation frameAnnotation = null;
	public boolean isNewLine = false;
	
	/** Creates new form FrameEditar */
	public FrameAddEdit(Object selectedLine, FrameTool frameTool, boolean isNewLine, String title, FrameAnnotation frameAnnotation) {
		initComponents();
		this.setTitle(title);
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		SpinnerModel spinnerModel = new SpinnerNumberModel(0.5, 0, 1, 0.05);
		confidence.setModel(spinnerModel);
		this.setFrameTool(frameTool);
		frameTool.enable(false);		
		this.frameAnnotation = frameAnnotation;
		this.isNewLine = isNewLine;
		rgFalsePositive.setSelected(true);
		buttonGroup.add(rgFalsePositive);
		buttonGroup.add(rgTruePositive);
		buttonGroup.add(rgFalseNegative);
		
		//Add new annotation false negative
		if (isNewLine){
			setEnabledRadioGroup(false);
			rgFalseNegative.setSelected(true);
			tfSubject.setText("");
			tfPredicate.setText("");
			tfObject.setText("");
			selectedAnnotation = new Annotation();
		}
		//Add new annotation true positive or false positive
		else if (selectedLine instanceof Triple){
			rgFalseNegative.setEnabled(false);				
			selectedTriple = new Triple();
			this.setSelectedTriple((Triple)selectedLine);	
			tfSubject.setText(selectedTriple.getSubject());
			tfPredicate.setText(selectedTriple.getPredicate());
			tfObject.setText(selectedTriple.getObject());
			setEditableTextFields(false);
		}
		//Edit annotation already exists
		else if (selectedLine instanceof Annotation){
			if (((Annotation) selectedLine).getType().equals("False negative")){
				setEnabledRadioGroup(false);
			}
			else{
				rgFalseNegative.setEnabled(false);	
			}			
			selectedAnnotation = new Annotation();
			this.setSelectedAnnotation((Annotation)selectedLine);			
			setEditableTextFields(true);
			tfSubject.setText(selectedAnnotation.getTriple().getSubject());
			tfPredicate.setText(selectedAnnotation.getTriple().getPredicate());
			tfObject.setText(selectedAnnotation.getTriple().getObject());
			if(selectedAnnotation.getType().equals("True positive")){
				rgTruePositive.setSelected(true);
			}
			else if (selectedAnnotation.getType().equals("False positive")){
				rgFalsePositive.setSelected(true);
			}
			else if (selectedAnnotation.getType().equals("False negative")){
				rgFalseNegative.setSelected(true);
			}
			confidence.setValue(Double.parseDouble(selectedAnnotation.getConfidence()));
		}
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        buttonGroup = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tfPredicate = new javax.swing.JTextField();
        tfSubject = new javax.swing.JTextField();
        tfObject = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        rgTruePositive = new javax.swing.JRadioButton();
        rgFalsePositive = new javax.swing.JRadioButton();
        confidence = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        rgFalseNegative = new javax.swing.JRadioButton();
        btnSave = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setText("Subject:");
        jLabel2.setText("Predicate:");
        jLabel3.setText("Object:");
        tfPredicate.setText("jTextField1");
        tfSubject.setText("jTextField2");
        tfObject.setText("jTextField3");
        tfObject.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Annotation"));
        rgTruePositive.setText("True positive");
        rgFalsePositive.setText("False positive");
        rgFalseNegative.setText("False negative");
        jLabel4.setText("Confidence:");
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(rgTruePositive)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(rgFalsePositive)
                        .addGap(2, 2, 2)
                        .addComponent(rgFalseNegative))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(confidence, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rgTruePositive)
                    .addComponent(rgFalsePositive)
                    .addComponent(rgFalseNegative))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(confidence, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnBack.setText("Back");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnSave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnBack))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(tfSubject, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfPredicate, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(tfObject, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)))
                        .addGap(18, 18, 18)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(16, 16, 16))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(tfSubject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(tfPredicate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(tfObject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBack)
                    .addComponent(btnSave))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>
    
	private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {                                          
		// TODO add your handling code here:
		frameTool.enable(true);	
		this.dispose();
	}                                         

	private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
		String evaluation = "";		
		if (rgTruePositive.isSelected())
			evaluation = "True positive";
		else if (rgFalsePositive.isSelected())
			evaluation = "False positive";
		else if (rgFalseNegative.isSelected())
			evaluation = "False negative";
		String conf = confidence.getValue().toString();
		//New line
		if (selectedTriple != null && selectedAnnotation == null){
			selectedAnnotation = new Annotation();
		}
		Triple triple = new Triple();
		triple.setSubject(tfSubject.getText());
		triple.setPredicate(tfPredicate.getText());
		triple.setObject(tfObject.getText());
		selectedAnnotation.setTriple(triple);
		selectedAnnotation.setIdQuery(frameTool.getIdQuery());
		selectedAnnotation.setIdResult(frameTool.getIdResult());
		selectedAnnotation.setType(evaluation);
		selectedAnnotation.setConfidence(conf);
		frameTool.addAnnotatedTriple(selectedAnnotation);
		if (frameAnnotation != null){
			if (!isNewLine){
				frameTool.getAnnotatedTriple().remove(frameAnnotation.tableAnnotation.getSelectedRow());
			}			
			frameAnnotation.restartTable();		
		}
		
		frameTool.enable(true);
		this.dispose();
	} 
	
	public void setEditableTextFields(boolean bool){
		tfSubject.setEditable(bool);
		tfPredicate.setEditable(bool);
		tfObject.setEditable(bool);
	}
	
	public void setEnabledRadioGroup(boolean bool){
		rgTruePositive.setEnabled(bool);
		rgFalsePositive.setEnabled(bool);
		rgFalseNegative.setEnabled(bool);
	}
	
	public Triple getSelectedTriple() {
		return selectedTriple;
	}

	public void setSelectedTriple(Triple selectedTriple) {
		this.selectedTriple = selectedTriple;
	}

	public FrameTool getFrameTool() {
		return frameTool;
	}

	public void setFrameTool(FrameTool frameTool) {
		this.frameTool = frameTool;
	}

	public Annotation getSelectedAnnotation() {
		return selectedAnnotation;
	}

	public void setSelectedAnnotation(Annotation selectedAnnotation) {
		this.selectedAnnotation = selectedAnnotation;
	}
}
